package com.sxhalo.fsms.controller.api;

import com.sxhalo.fsms.controller.BaseController;
import com.sxhalo.fsms.domain.*;
import com.sxhalo.fsms.export.ExcelUtils;
import com.sxhalo.fsms.export.ExportSetInfo;
import com.sxhalo.fsms.log.SysControllerLog;
import com.sxhalo.fsms.service.IDeviceLinkageRecordService;
import com.sxhalo.fsms.util.ApiResult;
import com.sxhalo.fsms.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api")
public class DeviceLinkageRecordController extends BaseController {

	@Autowired
	private IDeviceLinkageRecordService recordService;

	@ResponseBody
	@RequestMapping(value = "/device/linkage/record", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public ApiResult findDevicelinkageRecord(Integer current, Integer pageSize, HttpServletRequest req) {
		String where = getWhere(req);
		String order = " create_time desc";

		return findList(current, pageSize, where, order, recordService);
	}





	//表头选择
	private List<String[]> createUsersExcelHead() {
		List<String[]> headlist = new ArrayList<String[]>();
		String[] row1 = { "ID","任务名称", "设备Id","设备名称","变量数据","通讯指令","反馈信息","创建时间"};
		headlist.add(row1);
		return headlist;
	}



	@ResponseBody
	@SysControllerLog(operationName = "删除设备缺陷记录", modelName = "设备缺陷记录")
	@RequestMapping(value = "/device/linkage/record", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
	public ApiResult delRecord(Integer id, HttpServletRequest req) {
		recordService.deleteByPrimaryKey(id);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@SysControllerLog(operationName = "批量删除设备缺陷记录", modelName = "设备缺陷记录")
	@RequestMapping(value = "/device/linkage/record/batchdel", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
	public ApiResult delRecord(String ids, HttpServletRequest req) {
		recordService.deleteByWhere(" id in ("+ids+")");
		return ApiResult.retOK(req);
	}



	@SysControllerLog(operationName = "导出", modelName = "联动记录")
	@RequestMapping(value = "/exportDeviceLinkageRecordExcel", method = RequestMethod.GET)
	public void deviceDownload(String keyword, HttpServletRequest req, HttpServletResponse response) throws IOException {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		String fileName="联动记录";
		String[] Userkeys = { 	"ID","linkageName","deviceId","deviceName","attrValue","message","reserve","createTime"};

		String where = getWhere(req);
		String order = " create_time desc";

		List<DeviceLinkageRecord> data = recordService.findPagerList(0,-1,where, order);
		ExportSetInfo info = new ExportSetInfo();
		List<Map<String, Object>> coalSalesList = createUserListRecord(data);
		info.setHeadNames(createUsersExcelHead());
		info.setDatalist(coalSalesList);
		info.setTitles(fileName);
		info.setFieldNames(Userkeys);
		info.setOut(os);
		ExcelUtils.export2Excel(info);
		ExcelUtils.ExcelDownload(fileName, response, os);

	}

	private List<Map<String, Object>> createUserListRecord(List<DeviceLinkageRecord> data) {
		List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
		for (DeviceLinkageRecord fd: data) {
			Map<String, Object> mapValue = new HashMap<String, Object>();
			mapValue.put("ID", fd.getId());
			mapValue.put("linkageName", fd.getLinkageName());
			mapValue.put("deviceId", fd.getDeviceId());
			mapValue.put("deviceName", fd.getDeviceName());
			mapValue.put("attrValue", fd.getAttrValue());
			mapValue.put("message", fd.getMessage());
			mapValue.put("reserve", fd.getReserve());
			mapValue.put("createTime", fd.getCreateTime()==null?"":DateUtil.sdf.format(fd.getCreateTime()));

			listmap.add(mapValue);
		}

		return listmap;
	}


	private String getWhere(HttpServletRequest req){

		String where = "";

		if (!isBlank(req.getParameter("linkageName"))) {
			where = "  b.`name` like '%" + req.getParameter("linkageName").trim() + "%'";
		}
		if (!isBlank(req.getParameter("deviceName"))) {
			where += (where.equals("")?"":" and ")+ "  c.device_name like '%" + req.getParameter("deviceName").trim() + "%'";
		}

		return where ;

	}

}
